package com.ntd.study.base.thread.lock;

import cn.hutool.core.lang.Console;

/**
 * 死锁场景
 */
public class DemoDeadlock {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();
    static Runnable runnable = () -> {
        synchronized (lock1) {
            Console.log(Thread.currentThread().getName() + ":获取到锁1 - 等待获取锁2");
            synchronized (lock2) {
                Console.log(Thread.currentThread().getName() + ":获取到锁2");
            }
        }
    };
    static Runnable runnable2 = () -> {
        synchronized (lock2) {
            Console.log(Thread.currentThread().getName() + ":获取到锁2 - 等待获取锁1");
            synchronized (lock1) {
                Console.log(Thread.currentThread().getName() + ":获取到锁1");
            }
        }
    };

    public static void main(String[] args) {
        new Thread(runnable).start();
        new Thread(runnable2).start();
    }
}
